package com.liuwei.msmall.ware.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @Classname MyRabbitMqConfig
 * @Description TODO
 * @Date 2020/7/20 19:51
 * @Created by 刘
 */
@EnableRabbit  //开启用于监听消息队列功能
@Configuration
public class MyRabbitMqConfig {

//
//    @Value("${stock.queue.msg.ttl}")
//    private String ttl;


    //配置成使用序列化为json的序列化机制
    @Bean
    public MessageConverter getMessageConverter(){
        return new Jackson2JsonMessageConverter();
    }

    //自动创建解锁库存的交换机和消息队列

    @Bean
    public Exchange stockEventExchange(){
        //String name, boolean durable, boolean autoDelete, Map<String, Object> arguments
        TopicExchange exchange = new TopicExchange("stock-event-exchange",true,false,null);
        return exchange;
    }

    @Bean
    public Queue stockDelayQueue(){

        //String name, boolean durable, boolean exclusive, boolean autoDelete,
        //@Nullable Map<String, Object> arguments
        Map<String, Object> arguments=new HashMap<>();
        arguments.put("x-dead-letter-exchange","stock-event-exchange");//死信交给哪个死信路由
        arguments.put("x-message-ttl",360000);//消息过期时间，默认是毫秒单位
        arguments.put("x-dead-letter-routing-key","stock.release");//路由键
        Queue queue = new Queue("stock.delay.queue",true,false,false,arguments);

        return queue;
    }
    @Bean
    public Queue stockReleaseQueue(){

        Queue queue = new Queue("stock.release.stock.queue",true,false,false,null);

        return queue;
    }
    @Bean
    public Binding stockLockedBinding(){
        //String destination, DestinationType destinationType, String exchange, String routingKey,
        //@Nullable Map<String, Object> arguments
        Binding binding = new Binding("stock.delay.queue",
                Binding.DestinationType.QUEUE,
                "stock-event-exchange",
                "stock.locked",null);

        return binding;
    }
    @Bean
    public Binding stockReleaseBinding(){
        Binding binding = new Binding("stock.release.stock.queue",
                Binding.DestinationType.QUEUE,
                "stock-event-exchange",
                "stock.release.#",null);

        return binding;
    }

    //创建出订单交换机与库存解锁队列的绑定关系
    @Bean
    public Binding orderReleaseOtherBinding(){
        Binding binding = new Binding("stock.release.stock.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.release.other.#",null);

        return binding;
    }

}
